<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">

            <!-- 放置表单 -->
            <el-form
              ref="formData"
              label-width="120px"
              style="margin-left: 120px; margin-top:30px"
              :model="formData"
              :rules="rules"
            >
              <el-form-item
                label="姓名:"
                prop="username"
              >
                <el-input
                  v-model="formData.username"
                  style="width:300px"
                />
              </el-form-item>
              <el-form-item
                label="密码:"
                prop="password2"
              >
                <el-input
                  v-model="formData.password2"
                  style="width:300px"
                  type="password"
                />
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  @click="hUpdateUserInfo"
                >更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <component :is="userInfoComponent" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <component :is="jobInfoComponent" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getProfile, saveUserDetail } from '@/api/user'
import userInfo from '../components/userInfo'
import JobInfo from '../components/jobInfo'
export default {
  components: {
    userInfo,
    JobInfo
  },
  data () {
    return {
      userInfoComponent: 'userInfo',
      jobInfoComponent: 'JobInfo',
      formData: {
        username: '',
        password2: ''
      },
      rules: {
        username: [{ required: true, message: '内容不能为空', trigger: 'blur' },
        { min: 2, max: 16, message: '2-16个字符之间', trigger: 'blur' }],
        password2: [{ required: true, message: '内容不能为空', trigger: 'blur' },
        { min: 6, max: 12, message: '密码在6-12位之间', trigger: 'blur' }]
      }
    }
  },
  created () {
    // console.log()
    this.loadUserInfo(this.$route.params.id)
  },
  methods: {
    async loadUserInfo (id) {
      this.formData = await getProfile(id)
      // console.log(res)
    },
    async hUpdateUserInfo () {
      // 1、验证表单
      // 2、发送请求
      // 3、触发提示
      try {
        await this.$refs.formData.validate()
        await saveUserDetail({ ...this.formData, password: this.formData.password2 })
        this.$message.success('更新成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

